package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.d_c.model.Customer;
import com.zr.d_c.model.Dish;
import com.zr.d_c.model.Order;
import com.zr.d_c.model.User;
import com.zr.d_c.service.BuyCartService;
import com.zr.d_c.service.CustomerService;
import com.zr.d_c.service.DishService;
import com.zr.d_c.service.OrderService;
import com.zr.d_c.serviceimpl.BuyCartServiceImpl;
import com.zr.d_c.serviceimpl.CustomerServiceImpl;
import com.zr.d_c.serviceimpl.DishServiceImpl;
import com.zr.d_c.serviceimpl.OrderServiceImpl;
/**
 * 购物车生成新的订单
 * @author Administrator
 *
 */
public class NewOrderController extends HttpServlet {

	CustomerService customerservice = new CustomerServiceImpl();
	DishService dishservice = new DishServiceImpl();
	BuyCartService buycartservice = new BuyCartServiceImpl();
	OrderService orderservice = new OrderServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = req.getSession();
		PrintWriter pw = resp.getWriter();
		User u = (User) session.getAttribute("currentUser");
		int uid = u.getUid();
		Customer c = customerservice.getCustomerByUid(uid);
		
		int cid = c.getCid();
		
		String bidstr = req.getParameter("bids");
		String sidstr = req.getParameter("sids");
		String dnamestr = req.getParameter("dnames");
		String dpricestr = req.getParameter("dprices");
		String bnumstr = req.getParameter("bnums");
		String didstr = req.getParameter("dids");

		
		String[] sids = sidstr.split(",");
		boolean flag = false;
		for (int i = 0; i < sids.length-1; i++) {
			if(!sids[i].equals(sids[i+1])){
				flag = true;
				break;
			}
		}
		if(flag){
			pw.write("0");
		}else{
			flag = false;
			String[] dids = didstr.split(",");
			String[] bnums = bnumstr.split(",");
			String[] dnames = dnamestr.split(",");
			for (int i = 0; i < dids.length; i++) {
				Dish d = dishservice.getDishByDid(Integer.parseInt(dids[i]));
				if(Integer.parseInt(bnums[i])>d.getDnum()){
					pw.write(dnames[i]+"数量不够,");
					flag = true;
				}
			}
			if(!flag){
				String[] dprices = dpricestr.split(",");
				String[] bids = bidstr.split(",");
				int sid = Integer.parseInt(sids[0]);
				
				int[] bid = new int[bids.length];
				
				StringBuffer odishies = new StringBuffer();
				int osumprices = 0;
				for (int i = 0; i < dnames.length; i++) {
					bid[i] = Integer.parseInt(bids[i]);
					odishies.append(dnames[i]);
					odishies.append(":");
					odishies.append(bnums[i]);
					odishies.append("份,");
					osumprices += Integer.parseInt(dprices[i])*Integer.parseInt(bnums[i]);
				}
				Date date = new Date();	
				SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
				Order o = new Order();
				o.setOsumprices(osumprices);
				o.setCid(cid);
				o.setOcreatetime(f.format(date));
				o.setOdishies(odishies.toString());
				o.setOisevaluate(0);
				o.setOissure(0);
				o.setOisdelete(1);
				o.setSid(sid);
				if(orderservice.addOrder(o)>0){
					for (int i = 0; i < dids.length; i++) {
						Dish d = dishservice.getDishByDid(Integer.parseInt(dids[i]));
						int dnum = d.getDnum();
						dishservice.modifyDnum(Integer.parseInt(dids[i]), dnum-Integer.parseInt(bnums[i]));
					}
					buycartservice.deleteBuyCartByBid(bid);
					pw.write("1");
				}
			}
		}
		pw.close();
	}
}
